package com.example.day5rxpermission;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "TAG";
    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.bt2)
    Button bt2;
    @BindView(R.id.bt3)
    Button bt3;
    @BindView(R.id.bt4)
    Button bt4;
    RxPermissions rxPermissions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        rxPermissions = new RxPermissions(this);
//        rxPermissions.isGranted()  //同意
//        rxPermissions.isRevoked(); //取消
//        rxPermissions.request();



    }

    @OnClick({R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt1:
                //是否同意
                if(rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                    Log.i(TAG, "onSubscribe: ---->好的嘻嘻");
                }else{
                    Log.i(TAG, "onSubscribe: ---->走开");
                }
                break;
            case R.id.bt2:
                //多权限组合请求
                rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA).subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        Log.i("TAG", "onNext:-------> " + aBoolean);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
                break;
            case R.id.bt3://取消权限
                if(rxPermissions.isRevoked(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                    Log.i(TAG, "onViewClicked: --->取消了");
                }else{
                    Log.i(TAG, "onViewClicked: ----》还在");
                }
                break;
            case R.id.bt4:   //其他申请方式
                rxPermissions.requestEach(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Observer<Permission>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Permission permission) {
                        Log.i("TAG", "onNext:-------> " + permission.name+"  "+permission.granted);
                    }




                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
                break;
        }
    }
}
